import ClassicEditor from '@ckeditor/ckeditor5-build-classic'
import lodash from 'lodash'

console.log(lodash.VERSION)

const arr = [1, 2, 3, 4, 5]

console.log(arr, lodash.chunk(arr, 3))

/**
 * @type {import('vue').default & import('vue').ComponentOptions}
 */
const Page = {
  data() {
    return {
      editor: null
    }
  },
  async mounted() {
    const el = document.querySelector('#index-editor')
    this.editor = await this.createEditor(el)
  },
  methods: {
    async createEditor(el) {
      const editor = await ClassicEditor.create(el, { language: 'zh-cn' })
      return editor
    },
    publishPost() {
      this.$refs.post.innerHTML = this.editor.getData()
    }
  }
}

/**
 * @param {Promise<void>[]} middleware
 */
export function compose(middleware) {
  return function(context, next) {
    let index = -1
    function dispatch(i) {
      if (i < index) {
        return Promise.reject(new Error('next() called multiple times'))
      }

      index = i

      let fn = middleware[i]

      if (i === middleware.length) fn = next
      if (!fn) return Promise.resolve()

      try {
        return new Promise(
          fn(context, function() {
            return dispatch(i + 1)
          })
        )
      } catch (err) {
        return Promise.reject(err)
      }
    }
    return dispatch(0)
  }
}

export default Page
